// api/user.js

const { request } = require('./core');

/**
 * 获取用户信息
 * @param {object} params - 请求参数
 * @returns {Promise}
 */
const getUserProfile = (params = {}) => {
  return request({
    url: '/user/getUserInfo',
    method: 'GET',
    data: params,
    showLoading: true,
  });
};

/**
 * 用户登录
 * @param {string} code - 微信登录code
 * @returns {Promise}
 */
const login = (code) => {
  return request({
    url: '/OpenAPI/getOpenid',
    method: 'GET',
    data: { code },
    showLoading: false,
    loadingTitle: '登录中...',
  });
};

/**
 * 新增用户
 * @param {string} user_name - 昵称
 * @param {string} avatar - 头像
 * @param {string} open_id - 微信ID
 * @returns {Promise}
 */
const add = (user_name, avatar, open_id) => {
  return request({
    url: '/user/add',
    method: 'POST',
    data: { user_name, avatar, open_id },
    showLoading: true,
    loadingTitle: '更新中...',
  });
};

/**
 * 更新用户信息
 * @param {string} user_name - 昵称
 * @param {string} avatar - 头像
 * @param {string} open_id - 微信ID
 * @returns {Promise}
 */
const update = (user_name, avatar, open_id) => {
  return request({
    url: '/user/update',
    method: 'POST',
    data: { user_name, avatar, open_id },
    showLoading: true,
    loadingTitle: '更新中...',
  });
};

// 导出用户相关的所有接口
module.exports = {
  getUserProfile,
  login,
  add,
  update
};
